package cac.fenjian.resource.outStoreroom.sevice;

import cac.fenjian.resource.outStoreroom.dao.OutStoreroomDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OutStoreroomService {
    @Autowired
    private OutStoreroomDao outStoreroomDao;
    @Value("${pathProperty.isTest}")
    private Boolean isTest;

    @Transactional
    //通过传入的图号、数量、移交条码得到缓存箱编码
    public List<String> outStoreroomQuery(HashMap map) {

        HashMap sqlMap = new HashMap();
        sqlMap.put("i_item_code", map.get("itemCode"));
        sqlMap.put("i_item_qty", map.get("deliveryQty"));
        sqlMap.put("i_ip", map.get("ip"));
        sqlMap.put("i_material_barcode", map.get("barcode"));
        sqlMap.put("o_Return_Int", null);
        sqlMap.put("o_Return_String", null);
//        outStoreroomDao.callBle(sqlMap);
        //2023-06-23 lqc
        if (isTest) {
            outStoreroomDao.searchWorkbinCodeTest(sqlMap);
        }else {
            outStoreroomDao.searchWorkbinCode(sqlMap);
        }

        log.info(sqlMap.toString());

        List list = new ArrayList();
        String tmpPalletCode = "";
        String pallet_code = (String) sqlMap.get("o_Return_String");
//        String[] test = pallet_code.split("\\,");
//
//        for(int i = 0;i<test.length;i++){
//            log.info(test[i]);
//        }
        int retCode = (int) sqlMap.get("o_Return_Int");
        if (retCode == 1) {
            list.add("1");
            list.add(sqlMap.get("o_Return_String"));
            return list;
        }
        while (pallet_code.indexOf(",") > 0) {
            tmpPalletCode = pallet_code.substring(0, pallet_code.indexOf(","));
            log.info("tmpPalletCode:++++" + tmpPalletCode + "indexOf" + pallet_code.indexOf(","));
            pallet_code = pallet_code.substring(pallet_code.indexOf(",") + 1, pallet_code.length());
            log.info("pallet_code:+++++++" + pallet_code);
            list.add(tmpPalletCode);
        }
        log.info("缓存箱出货:" + list);

        return list;
    }

    //通过缓存箱编码在货架上找箱子
    public HashMap outStoreroomWorkbin(HashMap map) {
        return outStoreroomDao.outStoreroomQuery(map);
    }

    //物料入库首次叫箱
    public List<HashMap> outStoreroomFirst(HashMap map) {
        return outStoreroomDao.outStoreroomFirst(map);
    }

    @Transactional
    //零件退货出库
    public HashMap partOutStoreroom(HashMap map) {
        return outStoreroomDao.partOutStoreroom(map);
    }

    //分拣缓存箱出库
    @Transactional
    public List<HashMap> sortStoreRoomOut(HashMap map) {
        return outStoreroomDao.sortStoreRoomOut(map);
    }

    //判断该箱子有没有未完成的任务
    public HashMap searchTask(HashMap map) {
        return outStoreroomDao.searchTask(map);
    }

    @Transactional
    //退货调用存储过程叫箱
    public List<String> callReturnOutWorkbin(HashMap map) {
        //1.查询当前库存数量及缓存箱号
        List<HashMap> nowWorkbinQtys = outStoreroomDao.nowWorkbinQty(map);
        //2.遍历缓存箱，累加数量直到能够满足退库数量
        List endWorkbinQtys = new ArrayList();
        int allWorkbinQty = 0;
        for(int i = 0;i<nowWorkbinQtys.size();i++){
            HashMap nowWorkbinQty = nowWorkbinQtys.get(i);
            int workbinQty = (int) nowWorkbinQty.get("itemQty");
            int returnQty = (int) map.get("returnQty");
            String workbinCode = (String) nowWorkbinQty.get("workbinCode");
            if(workbinQty < returnQty){
                allWorkbinQty = workbinQty+allWorkbinQty;
                endWorkbinQtys.add(workbinCode);
                continue;
            }
            endWorkbinQtys.add(workbinCode);
        }
        return endWorkbinQtys;
    }

    /*判断是不是大件物料*/
    public HashMap isBulkyItems(String itemCode) {
        return outStoreroomDao.isBulkyItems(itemCode);
    }

    //插入大件货物进入到箱子临时表
    public int insertWorkbinDetal(HashMap map) {
        return outStoreroomDao.insertWorkbinDetal(map);
    }

    /*巷道开关*/
    @Transactional
    public int rowTurn(HashMap map) {
        return outStoreroomDao.rowTurn(map);
    }

    /*查询巷道是否被占用*/
    public String isRowOn(Map map) {
        return outStoreroomDao.isRowOn(map);
    }

    /*修改货架表数据*/
    @Transactional
    public int updateMsshevle(HashMap map) {
        return outStoreroomDao.updateMsshevle(map);
    }

    /*手动完成正在执行的任务*/
    public int taskFinish(HashMap map) {
        outStoreroomDao.shelvesStatus(map);
        return outStoreroomDao.taskFinish(map);
    }


    public int checkIsReceConsole(String ip){
       return outStoreroomDao.checkIsReceConsole(ip);
    }
}
